#include <vector>

using std::vector;

#include <vector>
#include <iostream>
using namespace std;

// 方法1：暴力
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int size = nums.size() + 1;
        int min = nums.size();
        for(int i = 0;i < size; ++ i)
        {
            int len = 0;
            int sum = 0;
            for(int j = i;j < size; ++ j)
            {
                sum += nums[j];
                ++ len;
                if(sum >= target)
                {
                    cout << "sum:" << sum << "\n"; 
                    min = len < min ? len : min;
                    break;
                }
            }
        }
        return min == nums.size() + 1 ? 0 : min;
    }
};

// 方法2：滑动窗口
class Solution2 {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int minLen = INT32_MAX;
        int size = nums.size();
        int sum = 0;
        int left = 0;
        for(int i = 0;i < size; ++ i)
        {
            sum += nums[i];
            while(sum >= target) // 移动窗口
            {
                minLen = minLen > (i - left + 1) ? (i - left + 1) : minLen;
                cout << "minLen:" << minLen << "\n";
                sum -= nums[left];
                ++ left;
            }
        }
        return minLen == INT32_MAX ? 0 : minLen;
    }
};

int main(int argc, char const *argv[])
{
    Solution2 s;
    vector<int> v = { 1,2,3,4,5 };
    cout << s.minSubArrayLen(15,v) << "\n";
    getchar();
    return 0;
}

